<template>
  <div>
    <PageTools>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="区域搜索">
          <el-input v-model="formInline.name" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="Search"
          >查询</el-button>
        </el-form-item>
      </el-form>
    </PageTools>
    <el-card class="main">
      <el-button
        style="margin-bottom: 10px"
        type="primary"
        size="small"
        icon="el-icon-circle-plus-outline"
        @click="addForm"
      >新建</el-button>
      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="list"
        style="width: 100%"
        :header-cell-style="{
          fontWeight: 'normal',
          background: '#f3f6fb',
        }"
      >
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column label="区域名称" prop="name" name="name" />
        <el-table-column label="点位数" prop="nodeCount" name="nodeCount" />
        <el-table-column label="备注说明" prop="remark" />
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="small"
              @click="detailsFn(row)"
            >查看详情</el-button>
            <el-button
              type="text"
              size="small"
              @click="editFn(row)"
            >修改</el-button>
            <el-button type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="space-between" align="middle">
        <el-col :span="19">
          <p class="paginationHeader">
            {{ `共${totalCount}条记录 第${page.pageIndex}/${totalPage}页` }}
          </p>
        </el-col>
        <el-col :span="5">
          <el-pagination
            background
            :current-page="page.pageIndex"
            :page-size="page.pageSize"
            :total="totalCount"
            layout="prev, next"
            prev-text="上一页"
            next-text="下一页"
            @current-change="changePage"
          />
        </el-col>
      </el-row>
    </el-card>
    <!-- 工单配置弹出框 -->
    <el-dialog
      class="dialog"
      title="新增区域"
      :visible="dialogRegion.dialogShow"
      width="45%"
      @close="closeDialog"
    >
      <el-form
        ref="RegionRef"
        label-width="120px"
        :model="dialogRegion.RegionForm"
        :rules="dialogRegion.RegionRules"
      >
        <el-form-item label="区域名称" prop="regionName">
          <el-input
            v-model="dialogRegion.RegionForm.regionName"
            type="text"
            placeholder="请输入内容"
            maxlength="15"
            show-word-limit
            style="width: 80%"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="dialogRegion.RegionForm.remark"
            placeholder="请输入备注(不超过40字)"
            style="width: 80%"
            type="textarea"
            :rows="3"
            maxlength="40"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancel" @click="closeDialog">取 消</el-button>
        <el-button
          type="primary"
          class="sure"
          @click="dialogRegionFn"
        >确 定</el-button>
      </div>
    </el-dialog>
    <!-- 区域详情 -->
    <DetailRegion :details-visible="detailsVisible" :item="regionObj" @closeDialog="closeDialogFn" />
  </div>
</template>

<script>
import DetailRegion from './component/detail-region.vue'
import { Regionsearch, addRegion, editRegion } from '@/api/node'
export default {
  components: {
    DetailRegion
  },
  data() {
    return {
      formInline: { name: '' },
      loading: false,
      list: [],
      page: {
        pageIndex: 1,
        pageSize: 10
      },
      totalCount: 0,
      totalPage: 0,
      dialogRegion: {
        dialogShow: false,
        RegionForm: {
          regionName: '',
          remark: ''
        },
        RegionRules: {
          regionName: [{ required: true, message: '请输入', trigger: 'blur' }],
          remark: [{ required: true, message: '请输入', trigger: 'blur' }]
        }
      },
      detailsVisible: false,
      regionObj: {}
    }
  },
  created() {
    this.Regionsearch()
  },
  methods: {
    async Regionsearch(name) {
      const { data } = await Regionsearch({ ...this.page, ...name })
      this.list = data.currentPageRecords
      this.totalCount = Number(data.totalCount)
      this.totalPage = Number(data.totalPage)
    },
    // 1.查询
    Search() {
      this.Regionsearch(this.formInline)
    },
    // 2.新增
    addForm() {
      this.dialogRegion.dialogShow = true
    },
    // 3.分页
    changePage(val) {
      this.page.pageIndex = val
      this.Regionsearch()
    },
    // 4.查看详情
    detailsFn(row) {
      this.regionObj = row
      this.detailsVisible = true
    },
    closeDialogFn() { this.detailsVisible = false },
    // 4.1.修改
    editFn(row) {
      this.dialogRegion.RegionForm.id = row.id
      this.dialogRegion.RegionForm.regionName = row.name
      this.dialogRegion.RegionForm.remark = row.remark
      this.dialogRegion.dialogShow = true
    },
    // 关闭弹出层取消和X
    closeDialog() {
      this.$refs.RegionRef.resetFields()
      this.dialogRegion.RegionForm = {
        regionName: '',
        remark: ''
      }
      this.dialogRegion.dialogShow = false
    },
    // 5.弹出层确定
    dialogRegionFn() {
      this.$refs.RegionRef.validate(async(isOk) => {
        if (!isOk) return
        if (this.dialogRegion.RegionForm.id) {
          await editRegion(this.dialogRegion.RegionForm)
        } else {
          await addRegion(this.dialogRegion.RegionForm)
        }
        this.Regionsearch()
        this.dialogRegion.dialogShow = false
      })
    }
  }
}
</script>
<style  lang="scss">
.el-form-item__content{
  margin-bottom: 15px;
}
</style>

